package com.travelshare.business.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.travelshare.business.domain.ColumnDetail;
import com.travelshare.business.domain.InfoCommunity;
import com.travelshare.business.domain.InfoSensitive;
import com.travelshare.business.domain.cache.CacheDataModel;
import com.travelshare.business.domain.cache.ConstantsCustom;
import com.travelshare.business.mapper.ColumnDetailMapper;
import com.travelshare.business.mapper.InfoCommunityMapper;
import com.travelshare.business.mapper.InfoSensitiveMapper;
import com.travelshare.business.utils.CustomCacheUtils;

//@EnableScheduling
@Service
@SuppressWarnings("unchecked")
public class CacheDataService {

	@Autowired
    private InfoSensitiveMapper infoSensitiveMapper;
	
	@Autowired
	private ColumnDetailMapper columnDetailMapper;
	
	@Autowired
    private InfoCommunityMapper infoCommunityMapper;
	
	/**
	 * 项目启动时，初始化数据到缓存
	 */
	@PostConstruct
	public void init() {

		// 敏感词
		cacheSensitive();
	}

	//@Scheduled(initialDelay=1000 * 60, fixedDelay = 1000 * 60 * 35)
	public void cacheData() {
		updateDetailCount();
		updateCommunityCount();
	}
	
	private void updateDetailCount() {
		Map<String, Integer> map = (Map<String, Integer>) CustomCacheUtils.getCacheDataMap(ConstantsCustom.detail_click_nums);
		if(!map.isEmpty()) {
			List<ColumnDetail> infoStrategyList = map.entrySet().stream().map(entry -> {
				ColumnDetail cm = new ColumnDetail();
				cm.setDetailId(Long.valueOf(entry.getKey().substring(entry.getKey().lastIndexOf("_") + 1)));
				cm.setClickNum(entry.getValue());
				return cm;
			}).collect(Collectors.toList());
			
			List<ColumnDetail> list = new ArrayList<>();
			for(ColumnDetail cm : infoStrategyList ) {
				if(cm.getClickNum() > 0) {
					list.add(cm);
				}
			}
			if(!list.isEmpty()) {
				columnDetailMapper.batchInfoColumnDetail(list);
			}
		}
	}
	
	private void updateCommunityCount() {
		Map<String, Integer> map = (Map<String, Integer>) CustomCacheUtils.getCacheDataMap(ConstantsCustom.community_click_nums);
		if(!map.isEmpty()) {
			List<InfoCommunity> infoCommunityList = map.entrySet().stream().map(entry -> {
				InfoCommunity cm = new InfoCommunity();
				cm.setCommunityId(Long.valueOf(entry.getKey().substring(entry.getKey().lastIndexOf("_") + 1)));
				cm.setClickNum(entry.getValue());
				return cm;
			}).collect(Collectors.toList());
			List<InfoCommunity> list = new ArrayList<>();
			for(InfoCommunity cm : infoCommunityList ) {
				if(cm.getClickNum() > 0) {
					list.add(cm);
				}
			}
			if(!list.isEmpty()) {
				infoCommunityMapper.batchInfoCommunity(list);
			}
		}
	}

	private void cacheSensitive() {
		try {
			List<InfoSensitive> list = infoSensitiveMapper.selectInfoSensitiveList(new InfoSensitive());
			if (!list.isEmpty()) {
				CacheDataModel data = new CacheDataModel();
				data.setKey(ConstantsCustom.sensitive_cache_key);
				data.setSensitives(list);
				cacheData(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void cacheData(CacheDataModel data) {
		CustomCacheUtils.removeCustomCache(data.getKey());
		CustomCacheUtils.setDataCache(data);
	}
}
